﻿#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <ui_dialog.h>
#include <QColorDialog>
#include <QFileDialog>



#define LINE_CHAR_NUMBER (56)

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = nullptr) : QDialog(parent), ui(new Ui::Dialog) {
        ui->setupUi(this);
        setWindowTitle(u8"页眉页脚设置窗口");
    }

    Dialog(QString defaultLogoPath, QString defaultLFooter, QString defaultRFooter) {
        ui = new Ui::Dialog;
        ui->setupUi(this);
        setWindowTitle(u8"页眉页脚设置窗口");

        if(!defaultLogoPath.isEmpty()) ui->lineEdit->setText(defaultLogoPath);

        if(!defaultLFooter.isEmpty()) {
            ui->textEdit_lFooter->setPlainText(defaultLFooter);
            lFooter = defaultLFooter;
        }
        else ui->textEdit_lFooter->setPlainText(lFooter);

        if(!defaultRFooter.isEmpty()) {
            ui->textEdit_rFooter->setPlainText(defaultRFooter);
            rFooter = defaultRFooter;
        }
        else ui->textEdit_rFooter->setPlainText(rFooter);

        // connect(ui->pushButton_color, &QPushButton::clicked, this, [=](){
        //     QColor color = QColorDialog::getColor(Qt::black, this, u8"选择颜色");
        //     if(color.isValid()){
        //         ui->pushButton_color->setStyleSheet(QString(u8"background-color: %1;").arg(color.name()));
        //         ui->pushButton_color->setPalette(QPalette(color));
        //     }
        // });
        connect(ui->pushButton_color, &QPushButton::clicked, this, [=](){
            QColor color = QColorDialog::getColor(Qt::black, this, u8"选择颜色");
            if(color.isValid()){
                QString style = QString("background-color: %1; border: 1px solid gray; border-radius: 4px;").arg(color.name());
                ui->pushButton_color->setStyleSheet(style);
            }
        });
        connect(ui->toolButton, &QToolButton::clicked, this, [=](){
            QString filePath = QFileDialog::getOpenFileName(this, u8"选择图片", QString(), u8"图片文件(*.png *.jpg *.bmp)");
            if(!filePath.isEmpty()){
                ui->lineEdit->setText(filePath);
            }
        });
    }

    ~Dialog(){ if(ui) delete ui; }


    QString getFontSetting() const {
        QString fontStr;
        fontStr += QString(u8R"(&")") + ui->fontComboBox->currentText() + QString(u8R"(")");
        if(ui->checkBox_bold->isChecked()) fontStr += QString(u8R"(&B)");
        fontStr += QString(u8R"(&)") + ui->fontSizeCombobox->currentText();
        fontStr += QString(u8R"(&K)") + ui->pushButton_color->palette().color(QPalette::Button).name().replace(u8R"(#)", "");
        return fontStr;
    }

    QString getHeader() const {
        QString resStr;
        return resStr;
    }

    QString getLFooter() const {
        return lFooter;
    }

    QString getRFooter() const {
        return rFooter;
    }

    QString getFooter() const {
        QString fontStr;
        fontStr += QString(u8R"(&")") + ui->fontComboBox->currentText() + QString(u8R"(")");
        if(ui->checkBox_bold->isChecked()) fontStr += QString(u8R"(&B)");
        fontStr += QString(u8R"(&)") + ui->fontSizeCombobox->currentText();
        fontStr += QString(u8R"(&K)") + ui->pushButton_color->palette().color(QPalette::Button).name().replace(u8R"(#)", "");

        QString t_lFooter = QString(u8R"(&L)") + fontStr + ui->textEdit_lFooter->toPlainText();
        QString t_rFooter = QString(u8R"(&R)") + fontStr + ui->textEdit_rFooter->toPlainText();

        QString lineStr;
        QChar ch(0x203E);
        for(auto i = 0; i < LINE_CHAR_NUMBER; ++i) {
            lineStr+=ch;
        }

        QString resStr = QString(u8R"(&C)") \
                         + u8R"(&"Calibri Light"&17)" + u8R"(&KAAAAAA)" \
                         + lineStr + fontStr + "\n" \
                         /*+ u8R"(&6)" + "\n\n"*/;
        resStr += t_lFooter + t_rFooter;

        return resStr;
    }


    QString getImagePath() const {
        return ui->lineEdit->text();
    }

private:
    Ui::Dialog *ui;
    QString lFooter = QString(u8"文档编号: \n文档名称: \n文档版本: ");
    QString rFooter = u8"&P/&N";
};

#endif // DIALOG_H
